#include "LibraryInterface.h"

SDL_Surface* LibraryInterface::screen;

LibraryInterface::LibraryInterface(){}
LibraryInterface::~LibraryInterface(){}

bool LibraryInterface::Initialize(){
	// Initialize SDL's subsystems - in this case, only video.
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) 
    {
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
        exit(1);
    }

    // Register SDL_Quit to be called at exit; makes sure things are
    // cleaned up when we quit.
    atexit(SDL_Quit);
    
    // Attempt to create a 640x480 window with 32bit pixels.
    screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    
    // If we fail, return error.
    if ( screen == NULL ) 
    {
        fprintf(stderr, "Unable to set 640x480 video: %s\n", SDL_GetError());
        exit(1);
    }
	
	
	
	return true;
}
bool LibraryInterface::Deinitialize(){

	return true;
}
void LibraryInterface::Print(char* output, int x, int y){

	
}
void LibraryInterface::putpixel(int x, int y, int color)
{
    unsigned int *ptr = (unsigned int*)screen->pixels;
    int lineoffset = y * (screen->pitch / 4);
    ptr[lineoffset + x] = color;
}

void LibraryInterface::render()
{   
    // Lock surface if needed
    if (SDL_LockSurface(screen) < 0) 
        return;

    // Ask SDL for the time in milliseconds
    int tick = SDL_GetTicks();

    // Declare a couple of variables
    int i, j, yofs, ofs;

    // Draw to screen
    yofs = 0;
    for (i = 0; i < 480; i++)
    {
        for (j = 0, ofs = yofs; j < 640; j++, ofs++)
        {
            ((unsigned int*)screen->pixels)[ofs] = i * i + j * j + tick;
        }
        yofs += screen->pitch / 4;
    }

    putpixel(10, 10, 0xff0000);
    putpixel(11, 10, 0xff0000);
    putpixel(10, 11, 0xff0000);
    putpixel(11, 11, 0xff0000);

    // Unlock if needed
    if (SDL_MUSTLOCK(screen)) 
        SDL_UnlockSurface(screen);

    // Tell SDL to update the whole screen
    SDL_UpdateRect(screen, 0, 0, 640, 480);    
}